<?php
include './Config.class.php';
class Record{
    public $phone;
    public $created_at;


    public function __construct(){

        date_default_timezone_set('Asia/Shanghai');

    }


    //检查奖项剩余数量
    public function Check($data,$type){

        if($type=='first'){
            if($data->award_first_num>0){

                return 1;
            }else{
                return 0;
            }
        }elseif($type=='second'){
            if($data->award_second_num>0){

                return 1;
            }else{
                return 0;
            }
        }else{
                return 0;//错误类型
        }



    }
    //摇奖
    public  function Rock(){
        //检查时间
        $type=$this->checkTime();
        if($type==-1){
            return $type;
        }

        //抽奖
        $num=rand(1,99);
        switch($num){
            case $num<1/10*100: //几率
                $_SESSION['rock']='1';
                return $this->Check(Config::getRecordNum(),$type);
            break;
            default:
                $_SESSION['rock']='0';
                return 0;
            break;
        }
    }
    //检查时间
    public static function checkTime(){
        $now= date('Y-m-d H:i:s');

        if(strtotime($now)>strtotime(Config::get('first_begin_time'))&&strtotime($now)<strtotime(Config::get('first_end_time'))) {

            return 'first';

        }elseif(strtotime($now)>strtotime(Config::get('second_begin_time'))&&strtotime($now)<strtotime(Config::get('second_end_time'))) {

            return 'second';

        }else{

            return -1;//现在不是抽奖时间

        }
    }

    //保存数据
    public function add($phone){

        $record=new Record();
        $record->created_at=date('Y-m-d H:i:s');
        $record->phone=$phone;

        $data=Config::getRecords();
        if(isset($data)){
            foreach($data as $re){
                if($re->phone==$phone){
                    return -2;//不能重复领奖
                }
            }
            array_push($data,$record);
        }
        if(!$_SESSION['rock']){
            return -3;
        }

        if(file_put_contents(Config::get('record_path'),json_encode($data))){
            $_SESSION['rock']='0';
            //更新数据
            $type=$this->checkTime();
            $data=Config::getRecordNum();
            if($type=='first'){
                if($data->award_first_num>0){
                    $data->award_first_num--;
                    file_put_contents(Config::get('data_path'),json_encode($data));
                }else{
                    return -4;
                }
            }else if($type=='second'){

                if($data->award_second_num>0){
                    $data->award_second_num--;
                    file_put_contents(Config::get('data_path'),json_encode($data));
                }else{
                    return -4;
                }

            }

            return 1;
        }else{
            return 0;
        }
    }
    //过滤器
    public static function Filter($str){
        $str=htmlspecialchars($str);
        $str=strip_tags($str);
        return $str;
    }


}

